unit MyFormatter;

{$mode objfpc}{$H+}

interface

uses Classes, SysUtils, MyTypes;

type

TFormatter = class
private
  Function GetGroup(str: string) : TGroup;
  Function GetLastMessageNumber(DataGroupString:string): integer;
  Function GetFirstMessageNumber(DataGroupString:string): integer;
  Function GetNameForGroup(DataGroupString:string): string;
  Function GetFirstWord(str:string): string;
protected
public
  procedure GetGroups(data: TStringList;var groups:TGroupList) ;
  function GetMessage(GroupId: integer; resultString: string; data:TStringList; cpage: string): TMessage;
  constructor Create();
end;


implementation

constructor TFormatter.Create();
begin
end;

procedure TFormatter.GetGroups(data: TStringList;var groups:TGroupList);
var i, k:integer; gr:TGroup;
begin
  k:=0;
  For i:=0 to data.Count-1 do
  begin
  {groups[i + 1]}
    gr:=self.GetGroup(data[i]);
    if(gr.Name <> '') then
    begin
      k:=k+1;
      SetLength(groups, k); {первый размер : 1}
      groups[k-1]:= gr;     {динамические нумеруются начиная с 0}
      WriteLn( gr.Name + ' ' + IntToStr(gr.FirstNumber) +  ' ' + IntToStr(gr.LastNumber ));
    end;
  end;
end;

Function TFormatter.GetGroup(str: string) : TGroup;
var gr: TGroup;
    lNumber, fNumber:integer; 
 begin
     gr:= TGroup.Create;
     lNumber:= GetLastMessageNumber(str);
     fNumber:= GetFirstMessageNumber(str);
     gr.Name :='';
    If (lNumber - fNumber > 0 )
       then
     Begin
        gr.Name:= GetNameForGroup(str);
	gr.FirstNumber := fNumber;
	gr.LastNumber := lNumber;
	gr.Date:= Now;
     end;
        Result:=gr;
 end;
 
Function TFormatter.GetLastMessageNumber(DataGroupString:string): integer;
{fido7.avp.support 0000022222 0000011111 m -> 22222}
begin
    Result:=StrToInt( copy(DataGroupString, pos(' ', DataGroupString)+1, 10) );
end; {funct}

Function TFormatter.GetFirstMessageNumber(DataGroupString:string): integer;
begin
{fido7.avp.support 0000022222 0000011111 m -> 11111}
    Result:=StrToInt( copy(DataGroupString, pos(' ', DataGroupString)+12, 10) );
end; {funct}

Function TFormatter.GetNameForGroup(DataGroupString:string): string;
begin
    Result := GetFirstWord(DataGroupString);
end; {funct}

Function TFormatter.GetFirstWord(str:string): string;
begin
     Result := copy(str, 1, pos(' ', str)-1);
end; {funct}

function TFormatter.GetMessage(GroupId: integer; resultString: string; data:TStringList; cpage: string): TMessage;
var mes:TMessage;
begin
     mes:= TMessage.Create;
     mes.GroupId:=GroupId;
     mes.Number:= StrToInt(GetFirstWord(resultString));
     mes.Head:= resultString;
     mes.Text:= data;
     mes.Codepage := cpage;

     Result:=mes;
end;

end.



